/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import entity.Product;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author haohao
 */
@ManagedBean
@SessionScoped
public class ShopingCart {

    private List<Product> cart;

    /** Creates a new instance of ShopingCart */
    public ShopingCart() {
        if (cart == null) {
            cart = new ArrayList<Product>();
        }
    }

    public List<Product> getCart() {
        return cart;
    }

    public void setCart(List<Product> cart) {
        this.cart = cart;
    }

    private String id;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String test() {
        //cach 1
        cart.add(new Product(id, name));

        //cach 2
//        List<Product> cart1;
//        cart1 = (List<Product>)FacesContext.getCurrentInstance().
//                getExternalContext().getSessionMap().get("newCart");
//        if (cart == null) {
//            cart1 = new ArrayList<Product>();
//            cart1.add(new Product(id, "afsdf"));
//            FacesContext.getCurrentInstance().
//                    getExternalContext().getSessionMap().put("newCart", cart1);
//        } else {
//            cart1.add(new Product(id, "afsdf"));
//        }
        return "viewcart.xhtml";
    }
}
